/**
 *====================================================
 * 文件名称: PickupDelayApplicationServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.PickupDelayApplicationEntity;
import com.yuexunit.finance.project.entity.lexicon.ApplicationSimpleStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;

/**
 * @ClassName: PickupDelayApplicationServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(PickupDelayApplicationService.class)
public class PickupDelayApplicationServiceImpl implements PickupDelayApplicationService {

	@Override
	public Long createPickupDelayApplication(PickupDelayApplicationEntity entity) {
		entity.setPickupDelayNumber(SettingConstants.PRE_NUMBER_DELAY_PICKUP + RandomUtil.generateTimeNumber());
		entity.setApplicationState(ApplicationSimpleStateEnum.NOT_APPROVE);
		entity.setAuditAmount(BigDecimal.ZERO);
		PersistUtil.insert(entity);
		return entity.getPickupDelayId();
	}

	@Override
	public void updatePickupDelayApplication(PickupDelayApplicationEntity entity) {
		PickupDelayApplicationEntity persist = inquirePickupDelayApplication(entity.getPickupDelayId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public PickupDelayApplicationEntity inquirePickupDelayApplication(Long pickupDelayId) {
		return PersistUtil.get(PickupDelayApplicationEntity.class, pickupDelayId);
	}

	@Override
	public PageList<PickupDelayApplicationEntity> inquirePickupDelayApplicationPageList(Long[] pickupDelayIds, Long enterpriseId, Long financeId, String financeNumber, ApplicationSimpleStateEnum applicationState, Date beginDate,
			Date endDate, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupDelayApplicationEntity.class);
		if (pickupDelayIds != null && pickupDelayIds.length > 0) {
			criteria.add(Restrictions.in("pickupDelayId", pickupDelayIds));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("applicantDateTime", endDate));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("applicantDateTime", beginDate));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (applicationState != null) {
			criteria.add(Restrictions.eq("applicationState", applicationState));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> countWaitAuditDelayApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupDelayApplicationEntity.class);
		criteria.add(Restrictions.eq("applicationState", ApplicationSimpleStateEnum.NOT_APPROVE));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("pickupDelayId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<PickupDelayApplicationEntity> inquireWaitPickupDelayApplication(Long pickupId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupDelayApplicationEntity.class);
		criteria.add(Restrictions.eq("pickupId", pickupId));
		criteria.add(Restrictions.eq("applicationState", ApplicationSimpleStateEnum.NOT_APPROVE));
		return PersistUtil.get(criteria);
	}

}
